/***************************
Customize ACCORDION Obj 
****************************/

// myAccordion = {
// 	settings : {
// 		// specify the class used for active (or open) accordion panels
// 	    active_class: 'active',
// 	    // allow multiple accordion panels to be active at the same time
// 	    multi_expand: false,
// 	    // allow accordion panels to be closed by clicking on their headers
// 	    // setting to false only closes accordion panels when another is opened
// 	    toggleable: true,
//   		locked_class: 'locked',
//   		callback : function () {}
//   	},
//   	events : function () {
//       var self = this;
//       var S = this.S;

//       S(this.scope)
//       .off('.fndtn.accordion')
//       .on('click.fndtn.accordion', '[' + this.attr_name() + '] > dd a.trigger', function (e) {

//       	var accordion = S(this).closest('[' + self.attr_name() + ']'),
//             groupSelector = self.attr_name() + '=' + accordion.attr(self.attr_name()),
//             settings = accordion.data(self.attr_name(true) + '-init'),
//             target = S('#' + this.href.split('#')[1]),
//             aunts = $('> dd', accordion),
//             siblings = aunts.find('.content'),
//             active_content = siblings.filter('.' + settings.active_class);
//         e.preventDefault();

//         if(target.parent().hasClass(settings.locked_class)){
// 			return;
//       	}

//         if (accordion.attr(self.attr_name())) {
//           siblings = siblings.add('[' + groupSelector + '] dd .content');
//           aunts = aunts.add('[' + groupSelector + '] dd');
//         }

//         if (settings.toggleable && target.is(active_content)) {
//           target.parents('dd').toggleClass(settings.active_class, false);

//         	  console.log(target)
        
//           target.toggleClass(settings.active_class, false);
//           settings.callback(target);
//           target.triggerHandler('toggled', [accordion]);
//           accordion.triggerHandler('toggled', [target]);
//           return;
//         }

//         if (!settings.multi_expand) {
//           siblings.removeClass(settings.active_class);
//           aunts.removeClass(settings.active_class);
//         }

//         target.addClass(settings.active_class).parents('dd').addClass(settings.active_class);
//         settings.callback(target);
//         target.triggerHandler('toggled', [accordion]);
//         accordion.triggerHandler('toggled', [target]);
//       });
//     }
// }

// $.extend( Foundation.libs.accordion, myAccordion );


// Foundation JavaScript
// Documentation can be found at: http://foundation.zurb.com/docs
$(document).foundation({
  tooltip: {
    tip_template : function (selector, content) {
      return '<span data-selector="' + selector + '" class="'
        + Foundation.libs.tooltip.settings.tooltip_class.substring(1)
        + '">' + content + '<span class="nub"></span><span class="nub border"></span></span>';
    }
  },
  abide : {
    live_validate : true,
    focus_on_invalid : false,
    error_labels: false // labels with a for="inputId" will recieve an `error` class
  }
});


var initModalBehaviours = function(){

	$(document).on('open.fndtn.reveal', '[data-reveal]', function () {
  		var modal = $(this);
  		modal.find('form')[0].reset();
	});

	setTableModalBehaviour($('#modale-importa'));
}

var setTableModalBehaviour = function(objs){
	objs.each(function(){
		
		var btn = $(this).find('a.button');
		var tds = $(this).find('table tr td');
		var inputs = tds.find('input[type="checkbox"]');

		inputs.click(function(){
			var ins = inputs.filter(':checked');
			var c = 'disabled';
			if(ins.length == 0){
				if(!btn.hasClass( c )) btn.addClass( c );
			}
			else btn.removeClass( c );
		})

		
		tds.children().click(function(event){
			var p = $(this).parent();
			var i = $(':checkbox', p);
			var ins = inputs.filter(':checked');
			var isnc = i.is(':not(:checked)');

			ins.click();
			if(isnc)
				i.click();
		})
	})
}

var initDettagliVeicoloCustomBehaviours = function(){
	var f = $('#dettagli-veicolo');
	var docRadio = f.find('input:radio[name="tipo-documento"]');
	var docRadioChecked = docRadio.filter(':checked');
	var labelDocRadios = f.find('.numero-documenti label');
	var inputWrappers = f.find('.upload-libretto')

	/** NUmero Documenti :: START **/

	// Default
	inputWrappers.hide().filter('[data-match="'+docRadioChecked.attr('data-check')+'"]').show();

	// Click
	labelDocRadios.click(function() {
		var rCheckedAfterClick = $(this).parent().find('input:radio');
		var rDC = rCheckedAfterClick.attr('data-check');

		inputWrappers.hide().filter('[data-match="'+rDC+'"]').show();
	});

	/** NUmero Documenti :: END **/
}

var disableFileUploadFields = function(){
  var fieldsToDisable = $('.foto-veicolo-box.loaded input');

  fieldsToDisable.attr('disabled', true); 
}



$(document).ready(function(){
	initModalBehaviours();
	initDettagliVeicoloCustomBehaviours();
  	disableFileUploadFields();
})

